package com.future.platform.common.config;

import com.future.platform.common.entity.ConfigDb;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import java.util.List;
import java.util.Map;

/**
 * mysql类
 * @author lijihong
 * @date 20201/1/14
 */
public class MySqlConfig {

    /**
     * 连接MySQL并返回JdbcTemplate
     * @param configDb
     * @return
     */
    public static JdbcTemplate getConnection(ConfigDb configDb){
        try {
            DriverManagerDataSource dataSource = new DriverManagerDataSource();
            dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
            dataSource.setUrl("jdbc:mysql://" + configDb.getUrl() + "?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2b8");
            dataSource.setUsername(configDb.getUser());
            dataSource.setPassword(configDb.getPassword());
            return new JdbcTemplate(dataSource);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

}
